#include "guiterm.hpp"

JobView::JobView(RemoteJob *owner, bool showHeader, string cmd)
	:ViewSegment(owner->getSession()), owner(owner)
{
	layout = new QVBoxLayout();
	layout->setSpacing(0);
	layout->setContentsMargins(0,0,0,0);
	setLayout(layout);
	
	if(showHeader)
	{
		headerLayout = new QHBoxLayout();
		headerLayout->setSpacing(0);
		headerLayout->setContentsMargins(0,0,0,0);
		layout->insertLayout(0, headerLayout, 0);
		
		string headerText = session->getPromptText() + cmd;
		headerLabel = new QLabel(QString(headerText.c_str()));
		headerLabel->setPalette(*PaletteManager::programView());
		headerLabel->setFont(config.font);
		headerLayout->addWidget(headerLabel);
		headerLayout->setStretchFactor(headerLabel, 1);
		
		string menuText = retprintf("%%%i", owner->getId());
		menuLink = new LinkWidget(menuText);
		connect(menuLink, SIGNAL(clicked()), this, SLOT(openContextMenu()));
		headerLayout->addWidget(menuLink);
	}
	else
	{
		headerLayout = NULL;
		headerLabel = NULL;
		menuLink = NULL;
	}
	
	setFocusPolicy(Qt::ClickFocus);
	
	QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
	sizePolicy.setHeightForWidth(true);
	setSizePolicy(sizePolicy);
}

JobView::~JobView()
{
	for(size_t ii=0; ii<segments.size(); ii++) {
		delete segments[ii];
		segments[ii] = NULL;
	}
	delete layout;
}

int JobView::heightForWidth(int w) const
{
	//int ret = QWidget::heightForWidth(w);
	int ret = layout->heightForWidth(w);
	return ret;
}

RemoteJob *JobView::getJob() const
	{ return owner; }

void JobView::jobFinished(int result)
{
	setFocusPolicy(Qt::NoFocus);
	
	if(segments.size())
		segments[segments.size()-1]->finished();
}

int JobView::getHeaderHeight()
{
	if(headerLayout)
		return headerLayout->sizeHint().height();
	else
		return 0;
}

bool JobView::isFocused() const
{
	// TODO
	return true;
}

void JobView::giveFocus()
{
	if(segments.size())
		segments[segments.size()-1]->setFocus();
	else
		setFocus();
}

int JobView::numSegments() const
{
	return segments.size();
}

ViewSegment *JobView::getSegment(int index)
{
	if(index<0 || index>=(int)segments.size())
		return NULL;
	return segments[index];
}

void JobView::addSegment(ViewSegment *segment)
{
	bool firstSegment = false;
	if(!segments.size()) firstSegment = true;
	
	segments.push_back(segment);
	layout->addWidget(segment);
	setFocusProxy(segment);
	
	segment->show();
	
	if(isFocused()) {
		segment->setFocus();
	} else {
		segments[segments.size()-1]->finished();
	}
}

void JobView::keyPressEvent(QKeyEvent *event)
{
	if(getWindow()->globalHotkey(event))
	{
		return;
	}
	if(event->key() == 'C' && event->modifiers() == Qt::ControlModifier)
	{
		owner->interrupt();
	}
	if(event->key() == 'C' && event->modifiers() == Qt::ControlModifier|Qt::ShiftModifier)
	{
		owner->terminate();
	}
	if(event->key() == 'Z' && event->modifiers() == Qt::ControlModifier)
	{
		owner->pause();
	}
	
	string text = event->text().toStdString();
	if(text.length())
		owner->sendData(text.c_str(), text.length());
}

void JobView::mousePressEvent(QMouseEvent *event)
{
	QWidget::mousePressEvent(event);
	
	if(event->button() == Qt::RightButton) {
		JobContextMenu *contextMenu = new JobContextMenu(owner);
		contextMenu->popup(mapToGlobal(event->pos()));
	}
}

void JobView::openContextMenu()
{
	JobContextMenu *contextMenu = new JobContextMenu(owner);
	
	QSize menuSize = contextMenu->sizeHint();
	QSize buttonSize = menuLink->size();
	QPoint botRightCorner = menuLink->pos() + QPoint(buttonSize.width(), buttonSize.height());
	QPoint globalBotRightCorner = mapToGlobal(botRightCorner);
	QPoint popupLocation = QPoint(globalBotRightCorner.x() - menuSize.width(), globalBotRightCorner.y());
	
	contextMenu->popup(popupLocation);
}

void JobView::moveToNewWindow()
{
	QSize windowSize = size();
	setParent(NULL);
	
	owner->getSession()->showPrompt(true);
	
	ProgramWindow *window = new ProgramWindow(this, windowSize);
	window->show();
}

